package com.adgox.tiantianbiting.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.TtbtApplication;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.ProgramDetailBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2019/3/11.
 */

public class ShareForFreeAdapter extends RecyclerView.Adapter<ShareForFreeAdapter.ViewHolder> implements View.OnClickListener {
    private List<ProgramDetailBean> datas;
    private Dialog shareDialog;
    private int userId;
    private int position;

    public ShareForFreeAdapter(List<ProgramDetailBean> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_share_for_free, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ProgramDetailBean bean = datas.get(position);
        holder.tvPosition.setText((position + 1) + "");
        Glide.with(holder.itemView.getContext()).load(bean.getShareImageUrl()).into(holder.ivBook);
        holder.tvBookName.setText(bean.getTitle());
        holder.tvDescribe.setText(bean.getDescribe());
        holder.tvNum.setText(bean.getListenNum() + "人免费拿");
        holder.tvShare.setTag(position);
        holder.tvShare.setOnClickListener(this);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void addDatas(List<ProgramDetailBean> beans) {
        datas.addAll(beans);
        notifyDataSetChanged();
    }

    public void clear() {
        datas.clear();
        notifyDataSetChanged();
    }

    public interface onItemClickListener{
        void onItemClick(View view,ViewName viewName,int position);
    }

    public onItemClickListener listener = null;

    public void setOnItemClickListener(onItemClickListener listener){
        this.listener = listener;
    }

    public enum ViewName{
        SHARE,
        ITEMVIEW
    }

    @Override
    public void onClick(View v) {
        userId = ((UserInfo) SessionManager.getDefault().getUser()).getId();
        if (v.getTag() != null) {
            position = (int) v.getTag();
        }

        if (v.getId() == R.id.tv_share){
            listener.onItemClick(v,ViewName.SHARE,position);
        } else {
            listener.onItemClick(v,ViewName.ITEMVIEW,position);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_position)
        TextView tvPosition;
        @BindView(R.id.iv_book)
        ImageView ivBook;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_share)
        TextView tvShare;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
